// by Xeno
// Commented/Edited by tyrghen

If (isNil "D_HALO_INITIATED") then {D_HALO_INITIATED = false;};
if (D_HALO_INITIATED) exitWith { diag_log "Halo Request already running";};
D_HALO_INITIATED = True;
// Performing the Eject action will automatically get the player out of the aircraft and create a parachute. That's actually how you can eject AIs.
// _vec = vehicle player;
// player action ["EJECT", vehicle player];
// player action ["EJECT", (_this select 0)];
// We set the height 5 meters under the actual height to be sure we're out of the way of the heli
// waituntil {_vec != vehicle player || ((vehicle player) isKindOf "ParachuteBase") || !alive player || (vehicle player == player) };
// _pos = _vec modelToWorld [0,-8,-10];
// player setPos _pos;
// player setvelocity [0,0,0];
// [player, ((player call XfGetHeight) - 7)] call XfSetHeight;
diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"halo.sqf","HALO", getPos player,HaloScript];
_obj_jump = player;
player allowDamage false;
[] spawn {
	sleep 10;
	player allowDamage true;
};

switch (HaloScript) do {
	case 1: {
		// _obj_jump setvelocity [0,0,0];
		_obj_jump action["EJECT",vehicle _obj_jump];
		waituntil {_uh60p != vehicle _obj_jump || ((vehicle _obj_jump) isKindOf "ParachuteBase") || !alive _obj_jump || (vehicle _obj_jump == _obj_jump) };
		_pos = _vec modelToWorld [0,-8,-10];
		_obj_jump setPos _pos;
		_obj_jump setvelocity [0,0,0];
		diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"halo.sqf","Player Position", getPos _obj_jump];
		if (alive _obj_jump) then {
			if (vehicle _obj_jump isKindOf "ParachuteBase") then {
				_vec = vehicle _obj_jump;
				_obj_jump action["EJECT",vehicle _obj_jump];
				if (_vec != _obj_jump) then { deleteVehicle _vec; };
			};
			[_obj_jump, _obj_jump call XfGetHeight] execVM "Scripts\AAHALO\Scripts\HALO_init.sqf";
		};
	};
	case 2: {
		_obj_jump action ["eject",_uh60p];
		if (vehicle _obj_jump isKindOf "ParachuteBase") then {
			_vec = vehicle _obj_jump;
			_obj_jump action["EJECT",vehicle _obj_jump];
			if (_vec != _obj_jump) then { deleteVehicle _vec; };
		};
		[_obj_jump, _obj_jump call XfGetHeight] execVM "scripts\HALO\HALOFall.sqf";
	};
	case 3: {
		_obj_jump action ["eject",_uh60p];
		[_obj_jump] exec "ca\air2\halo\data\Scripts\HALO_getout.sqs";
	};
	default {
		if (isnil "bis_fnc_halo") then {
			bis_fnc_halo = compile preprocessFileLineNumbers "scripts\AAHALO\Scripts\fn_halo.sqf";
		};
		// _obj_jump execVM "Scripts\AAHALO\Scripts\fn_halo.sqf";
		if (vehicle _obj_jump != _obj_jump) then {
			_obj_jump action ["eject",vehicle _obj_jump];
		};
		// [_obj_jump, ((vehicle _obj_jump) call XfGetHeight) - 10] spawn bis_fnc_halo;
		_obj_jump spawn bis_fnc_halo;
		// _obj_jump action ["eject",_uh60p];
		diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"halo.sqf","(NEW DOM) Player Position", getPos _obj_jump, vehicle _obj_jump];
	};
};

// We check if the player has a parachute, if he does, then we delete it (otherwise he can't HALO)
// if ((vehicle player) isKindOf "ParachuteBase") then {
	// diag_log Format["HALO: deleting chute: %1",vehicle player];
	// _vec = vehicle player;
	// player action ["EJECT",vehicle player];
	// if (_vec != player) then deleteVehicle _vec;
// };
// diag_log Format["HALO: calling HALO_Init: %1",player call XfGetHeight];
// [player, player call XfGetHeight] execVM "Scripts\AAHALO\Scripts\HALO_init.sqf";

// R3F Halo Script
// private ["_vehicle","_unit"];
// _vehicle = _this select 0;
// _unit = _this select 1;
// _vehicle removeAction (_this select 2);
// if (_vehicle == _unit) exitWith {
	// _vehicle = createVehicle ["Parachute_US_EP1",position _unit,[],0,"FLY"];
	// _unit moveIndriver _vehicle;
// };
// _unit action ["eject",_vehicle];
// [_unit] exec "ca\air2\halo\data\Scripts\HALO_getout.sqs";

sleep 2;
D_HALO_INITIATED = False;